package chen.web.forum;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import chen.web.DataSourceProvider;
import chen.web.util.DateInt;

class CategoryDynamicInfoManager {
	
	private static SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(DataSourceProvider.getDataSource());
	private static final String SQL_1 = "update web_category_dynamic set cat_postCount = ?, cat_subjectCount = ?, cat_todayPostCount = ?, cat_yesterdayPostCount = ?, cat_lastPostAuthorId = ?, cat_lastPostAuthorNickname = ?, cat_lastPostDate = ? where cat_id = ?";
	private static final String SQL_2 = "insert into web_category_dynamic(cat_id) value(?)";
	private static final String SQL_3 = "select * from web_category_dynamic where cat_id = ?";
	
	static void loadDynamicInfo(Category category){
		CategoryDynamicInfo info = null;
		try{
			info = simpleJdbcTemplate.queryForObject(SQL_3, new DynamicInfoRowMapper(), category.getId());			
		}catch(EmptyResultDataAccessException ex){
			info = new CategoryDynamicInfo();
			simpleJdbcTemplate.update(SQL_2, category.getId());
		}
		category.setDynamicInfo(info);
	}
	
	
	static void updateDynamicInfo(Category category){
		CategoryDynamicInfo info = category.getDynamicInfo();
		simpleJdbcTemplate.update(SQL_1, info.getPostCount(), info.getSubjectCount(), info.getTodayPostCount(), info.getYesterdayPostCount(), info.getLastPostAuthorId(), info.getLastPostAuthorNickname(), DateInt.transform(info.getLastPostDate()), category.getId());
	}
	
	private static class DynamicInfoRowMapper implements RowMapper<CategoryDynamicInfo>{
		@Override
		public CategoryDynamicInfo mapRow(ResultSet rs, int i)
				throws SQLException {
			// TODO Auto-generated method stub
			CategoryDynamicInfo info = new CategoryDynamicInfo();
			info.setPostCount(rs.getInt("cat_postCount"));
			info.setSubjectCount(rs.getInt("cat_subjectCount"));
			info.setTodayPostCount(rs.getInt("cat_todayPostCount"));
			info.setYesterdayPostCount(rs.getInt("cat_yesterdayPostCount"));
			info.setLastPostAuthorId(rs.getInt("cat_lastPostAuthorId"));
			info.setLastPostAuthorNickname(rs.getString("cat_lastPostAuthorNickname"));
			info.setLastPostDate(DateInt.transform(rs.getInt("cat_lastPostDate")));
			return info;
		}
		
	}
	
	static CategoryDynamicInfo dynamicInfo(Category cat, int deep) {
		CategoryDynamicInfo info = childrenDynamicInfo(cat, deep);
		merge(cat.getDynamicInfo(), info);
		return info;
	} 
	
	private static CategoryDynamicInfo childrenDynamicInfo(Category category, int deep){
		List<Category> children = category.getChildren();
		if(children.size() == 0 || deep == 0){
			return new CategoryDynamicInfo();	
		} else {
			CategoryDynamicInfo info = new CategoryDynamicInfo();
			for(Category child : children){
				CategoryDynamicInfo childInfo = child.getDynamicInfo();
				merge(childInfo, info);
				merge(childrenDynamicInfo(child, deep-1), info);
			}
			return info;
		}
	}
	
	/**
	 * 合并栏目动态信息，从源栏目信息向目标栏目信息合并
	 * 帖子数量使用加法。最后的帖子信息取较后发布的
	 * @param from	源栏目信息
	 * @param to	目标栏目信息
	 */
	private static void merge(CategoryDynamicInfo from, CategoryDynamicInfo to){
		to.setSubjectCount(from.getSubjectCount() + to.getSubjectCount());
		to.setPostCount(from.getPostCount() + to.getPostCount());
		to.setTodayPostCount(from.getTodayPostCount() + to.getTodayPostCount());
		to.setYesterdayPostCount(from.getYesterdayPostCount() + to.getYesterdayPostCount());
		if(from.getLastPostDate().after(to.getLastPostDate())){
			to.setLastPostAuthorId(from.getLastPostAuthorId());
			to.setLastPostAuthorNickname(from.getLastPostAuthorNickname());
			to.setLastPostDate(from.getLastPostDate());
		}
	}
}
